import { tabBar } from "../pages.json"
export default () => {
    const navigateTo = ({ url }) => {
        const index = tabBar.list.findIndex((p) => {
            return url === "/" + p.pagePath
        })
        index >= 0 ?
            uni.switchTab({
                url: url
            })
            :
            uni.navigateTo({
                url: url
            });
    }

    const redirectTo = ({ url }) => {
        const index = tabBar.list.findIndex((p) => {
            return url === "/" + p.pagePath
        })
        index >= 0 ?
            uni.switchTab({
                url: url
            })
            :
            uni.redirectTo({
                url: url
            });
    }

    const goBack = () => {
        uni.navigateBack()
    }

    const checkSession = (provider) => {
        return new Promise((resolve, reject) => {
            uni.checkSession({
                success: res => {
                    resolve(res);
                },
                fail: err => {
                    reject(err);
                },
                complete() {
                }
            })
        })
    }

    const login = (provider) => {
        return new Promise((resolve, reject) => {
            uni.login({
                provider: provider || "weixin",
                success: res => {
                    resolve(res);
                },
                fail: err => {
                    reject(err);
                }
            });
        })
    }

    const getUserInfo = (provider) => {
        return new Promise((resolve, reject) => {
            uni.getUserInfo({
                provider: provider || "weixin",
                success: res => {
                    resolve(res);
                },
                fail: err => {
                    reject(err);
                }
            });
        })
    }


    const getProvider = (provider) => {
        return new Promise((resolve, reject) => {
            uni.getProvider({
                service: 'oauth',
                success: res => {
                    resolve(res);
                },
                fail: err => {
                    reject(err);
                }
            });
        })
    }


    function setStorage(key, data) {
        uni.setStorage({
            // key: 'storage_key',
            // data: 'hello',
            key,
            data,
            success: function () {
                console.log(key, 'setStorage success');
            },
            fail() {
                console.log(key, 'setStorage fail');
            }
        });
    }

    const payment = (data) => {
        return new Promise((resolve) => {
            uni.requestPayment({
                provider: 'wxpay',
                appid: data.appId,
                timeStamp: data.timeStamp,
                nonceStr: data.nonceStr,
                package: data.package,
                signType: data.signType,
                paySign: data.paySign,
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    resolve(err)
                }
            });
        })
    }

    return {
        checkSession, login, getUserInfo, getProvider, setStorage, navigateTo, redirectTo, goBack, payment
    }
}
